//
//  BTEHomeView.swift
//  BTE
//
//  Created by Mike Zhang on 2025/8/5.
//

import UIKit

class BTEHomeView: UIView {
    
    var clickkf:((String)->())?
    
    var lickcpro:((String)->())?
    
    var clickurlx:((String)->())?
    
    var clickurld:((String)->())?
    
    var clicklist:((String)->())?
    
    var load:(()->())?
    
    var refreshfunc: UIRefreshControl!
    
    var model: BTsidelingwiseModel?{
        didSet {
            list.reloadData()
        }
    }

    let bg = UIImageView.init(image: UIImage.init(named: "Maskfewfwefewfewf groupfewfe"))
    
    let list = BTEBaseTableView.init(frame: .zero, style: .plain)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(bg)
        bg.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        list.showsHorizontalScrollIndicator = false
        list.backgroundColor = .clear
        addSubview(list)
        list.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalToSuperview().offset(-106)
        }
        list.delegate = self
        list.dataSource = self
        list.separatorStyle = .none
        
        list.register(BTEBATableViewCell.self, forCellReuseIdentifier: BTEBATableViewCell.indetification)
        list.register(BTEBBTableViewCell.self, forCellReuseIdentifier: BTEBBTableViewCell.indetification)
        list.register(BTEBCTableViewCell.self, forCellReuseIdentifier: BTEBCTableViewCell.indetification)
        list.register(BTEListATableViewCell.self, forCellReuseIdentifier: BTEListATableViewCell.indetification)
        list.register(BTEListBTableViewCell.self, forCellReuseIdentifier: BTEListBTableViewCell.indetification)
        list.register(BTEOrderTableViewCell.self, forCellReuseIdentifier: BTEOrderTableViewCell.indetification)
        
        refreshfunc = UIRefreshControl()
        refreshfunc.addTarget(self, action: #selector(loadInfoaction), for: .valueChanged)
        list.refreshControl = refreshfunc
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    @objc func loadInfoaction(){
        load?()
    }

}



extension BTEHomeView: UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let m = model?.vincents {
            let list = m.filter({$0.dikegrave! == "noncomplacent"})
            if (list.first?.tiniest ?? []).count > 0 {
                return 1 + (list.first?.tiniest ?? []).count
            } else {
                return 3
            }
        }
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let m = model?.vincents {
            let list = m.filter({$0.dikegrave! == "noncomplacent"}).first?.tiniest ?? []
            if list.count > 0 {
                if indexPath.row == 0 {
                    guard let la = tableView.dequeueReusableCell(withIdentifier: BTEListATableViewCell.indetification, for: indexPath) as? BTEListATableViewCell else {
                        return UITableViewCell()
                    }
                    
                    la.model = m.filter({$0.dikegrave! == "reposes"}).first?.tiniest?.first
                    
                    
                    
                    return la
                } else {
                    guard let lb = tableView.dequeueReusableCell(withIdentifier: BTEListBTableViewCell.indetification, for: indexPath) as? BTEListBTableViewCell else {
                        return UITableViewCell()
                    }
                    let item = list[indexPath.row-1]
                    lb.model = item
                    return lb
                }
            } else {
                if indexPath.row == 0 {
                    guard let a = tableView.dequeueReusableCell(withIdentifier: BTEBATableViewCell.indetification, for: indexPath) as? BTEBATableViewCell else {
                        return UITableViewCell()
                    }
                    
                    a.model = m.filter({$0.dikegrave! == "tamarao"}).first?.tiniest?.first
                    
                    a.pro = model?.earworms?.aftermath
                    
                    a.rightima.isUserInteractionEnabled = true
                    let kef  = UITapGestureRecognizer.init(target: self, action: #selector(clickkfd))
                    a.rightima.addGestureRecognizer(kef)
                    
                    a.otherbu.addTarget(self, action: #selector(clickpro), for: .touchUpInside)
                    
                    tableView.reloadRows(at: [indexPath], with: .none)

                    return a
                } else if indexPath.row == 1 {
                    guard let b = tableView.dequeueReusableCell(withIdentifier: BTEBBTableViewCell.indetification, for: indexPath) as? BTEBBTableViewCell else {
                        return UITableViewCell()
                    }
                    return b
                } else {
                    guard let c = tableView.dequeueReusableCell(withIdentifier: BTEBCTableViewCell.indetification, for: indexPath) as? BTEBCTableViewCell else {
                        return UITableViewCell()
                    }
                    return c
                }
            }
        }
        return UITableViewCell()
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            if let m = model?.vincents {
                let list = m.filter({$0.dikegrave! == "noncomplacent"}).first?.tiniest ?? []
                if list.count > 0 {
                    clickurlx?(m.filter({$0.dikegrave! == "reposes"}).first?.tiniest?.first?.archipallium ?? "")
                } else {
                    clickurld?(m.filter({$0.dikegrave! == "tamarao"}).first?.tiniest?.first?.archipallium ?? "")
                }
            }
        } else {
            if let m = model?.vincents {
                let list = m.filter({$0.dikegrave! == "noncomplacent"}).first?.tiniest ?? []
                if list.count > 0 {
                    clicklist?(list[indexPath.row-1].archipallium ?? "")
                }
            }
           
        }
    }
    
    @objc func clickkfd(){
        clickkf?(model?.opinator?.oversoaking ?? "")
    }
    
    @objc func clickpro(){
        lickcpro?(model?.earworms?.plectra ?? "")
    }
    

}
